package com.croofr.utils.array;

import com.croofr.utils.exceptions.DefenseException;
import com.croofr.utils.object.ObjectDefense;
import com.croofr.utils.string.StringDefense;

/**
 * Defense checkers for Array.
 * <p/>
 * User: bednar<br/>
 * Date: Jan 24, 2010<br/>
 * Time: 1:11:20 PM
 */
public final class ArrayDefense
{
    /**
     * Throw DefenseException if array is null.
     *
     * @param array for check
     * @param <T>   type of array elements
     * @see com.croofr.utils.exceptions.DefenseException
     */
    public static <T> void notNull(T[] array)
    {
        if (array == null)
        {
            throw new DefenseException("Array is null!");
        }
    }


    /**
     * Throw DefenseException if size of array isn't equals parameter size.
     *
     * @param array for check
     * @param size  for check
     * @param <T>   type of array elements
     * @see com.croofr.utils.exceptions.DefenseException
     */
    public static <T> void equalsSize(T[] array, Integer size)
    {
        ObjectDefense.notNull(size);
        notNull(array);

        if (array.length != size)
        {
            throw new DefenseException("Array has wrong size.");
        }
    }

    /**
     * Throw DefenseException if any elements of array is null or array is null.
     *
     * @param array for check
     * @param <T>   type of array elements
     * @see com.croofr.utils.exceptions.DefenseException
     */
    public static <T> void allNotNull(T[] array)
    {
        notNull(array);

        for (T t : array)
        {
            ObjectDefense.notNull(t);
        }
    }

    /**
     * Throw DefenseException if any String of array is empty or array is null.
     *
     * @param array for check
     * @see com.croofr.utils.exceptions.DefenseException
     */
    public static void allNotEmpty(String[] array)
    {
        notNull(array);

        for (String string : array)
        {
            StringDefense.notEmpty(string);
        }
    }
}
